package se.id.vrapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

public class PlanRaceFragment extends Fragment {

	public final static String DEPOT_ID = "DEPOT_ID";
	public static int[] pauseTimes;
	public static String[] depotNames;
	public static int totalTime, totalTimeFix;
	private View v;
	ListView lv;

    static PlanRaceFragment newInstance(int num) {
    	PlanRaceFragment f = new PlanRaceFragment();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
       v = inflater.inflate(R.layout.activity_plan_race, container, false);
        ///
    	totalTimeFix = 420;
		lv = (ListView) v.findViewById(R.id.list);
		final Intent intent = new Intent(getActivity(), PlannerDialogActivity.class);

		depotNames = getResources().getStringArray(R.array.planner_depots);
		pauseTimes = new int[depotNames.length];

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				intent.putExtra(DEPOT_ID, position);
				startActivity(intent);
			}
		});

		lv.setAdapter(new PlannerDepotListAdapter(getActivity().getApplication(), depotNames));

		SeekBar totalTimeBar = (SeekBar) v.findViewById(R.id.planner_seekbar_totaltime);
		final TextView totaltimeTextView = (TextView) v.findViewById(R.id.planner_text_totaltime);
		final TextView averageSpeedTextView = (TextView) v.findViewById(R.id.planner_average_speed);
		totaltimeTextView
				.setText("" + getTimeVersion(totalTime + totalTimeFix));
		totalTimeBar.setProgress(totalTime);

		totalTimeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				totalTime = seekBar.getProgress()*5;
				totaltimeTextView.setText(""
						+ getTimeVersion(totalTime + totalTimeFix));
				averageSpeedTextView
						.setText(getAverageSpeedVersion(getAverageSpeed()));
				StatusFragment.plannedSpeed = (int) Math.ceil(getAverageSpeed());
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
        
        return v;
    }


	private double getAverageSpeed() {

		double distance = 300;

		double pt = 0;
		for (int i : pauseTimes) {
			pt += i;
		}

		double tt = totalTime + totalTimeFix - pt;
		double ret = distance / (tt / 60d);

		ret = Math.round(ret * 10.0) / 10.0;

		return ret;
	}

	private String getTimeVersion(int minutes) {
		int hours = minutes / 60;
		int min = minutes % 60;
		if (min < 10)
			return hours + ":0" + min;
		else
			return hours + ":" + min;
	}

	private String getAverageSpeedVersion(double speed) {
		return speed + " km/h";
	}

	@Override
	public void onResume() {
		super.onResume();
		((ArrayAdapter) lv.getAdapter()).notifyDataSetChanged();
		((TextView) v.findViewById(R.id.planner_average_speed))
				.setText(getAverageSpeedVersion(getAverageSpeed()));
		StatusFragment.plannedSpeed = (int) Math.ceil(getAverageSpeed());
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		pauseTimes = null;
		depotNames = null;
	}
}
